﻿using System.Configuration;
using System.Windows.Forms;
using FileSquirrel.Logic;
using FileSquirrel.Properties;

namespace FileSquirrel
{
    public partial class UserSettings : Form
    {
        private bool isFirstRun;

        public UserSettings(bool _isFirstRun)
        {
            InitializeComponent();
            isFirstRun = _isFirstRun;
        }

        private void ClearControls()
        {
            txt_Path.Text = string.Empty;
            txt_Extensions.Text = string.Empty;
        }

        private void ToggleButtons(bool edit)
        {
            btn_Update.Enabled = edit;
            btn_Delete.Enabled = edit;
        }

        private bool CheckUserInput()
        {
            bool pass = true;

            if (!System.IO.Directory.Exists(txt_Path.Text))
            {
                MessageBox.Show("Path does not exist.", "Invalid Path", MessageBoxButtons.OK, MessageBoxIcon.Error);
                pass = false;
            }

            if (cmbCategory.SelectedIndex == -1)
            {
                MessageBox.Show("You have to enter a category.", "Empty Category", MessageBoxButtons.OK, MessageBoxIcon.Error);
                pass = false;
            }

            if (string.IsNullOrEmpty(txt_Extensions.Text))
            {
                MessageBox.Show("You have to enter at lease one extension.", "Empty Extensions", MessageBoxButtons.OK, MessageBoxIcon.Error);
                pass = false;
            }

            return pass;
        }

        #region Event Handlers

        private void UserSettings_Load(object sender, System.EventArgs e)
        {
            Categories_DGV.DataSource = Reader.UserSettings.Category;
            txtLibName.Text = Settings.Default.LibraryName;
            txtLibPath.Text = Settings.Default.LibraryDir;
        }

        private void btn_GetPath_Click(object sender, System.EventArgs e)
        {
            if (txt_Path.Text != string.Empty)
                folderBrowserDialog_Path.SelectedPath = txt_Path.Text;

            if (folderBrowserDialog_Path.ShowDialog(this) == DialogResult.OK)
                txt_Path.Text = folderBrowserDialog_Path.SelectedPath;
        }

        private void btn_Add_Click(object sender, System.EventArgs e)
        {
            if (btn_Add.Text == "Add")
            {
                if (CheckUserInput())
                {
                    Logic.XSD.UserSettings.CategoriesRow catsRow;

                    if (Reader.UserSettings.Categories.Count == 0)
                        catsRow = Reader.UserSettings.Categories.AddCategoriesRow();
                    else
                        catsRow = Reader.UserSettings.Categories[0];

                    Logic.XSD.UserSettings.CategoryRow catRow = Reader.UserSettings.Category.AddCategoryRow(cmbCategory.SelectedItem.ToString(), txt_Path.Text, txt_Extensions.Text, catsRow);

                    Reader.UserSettings.WriteXml(System.IO.Path.GetFullPath("XML/UserSettings.xml"));

                    ClearControls();
                }
            }
            else if (btn_Add.Text == "New")
            {
                btn_Add.Text = "Add";
                ClearControls();
                ToggleButtons(false);
                AutoAssignExtensions();
            }
        }

        private void btn_Update_Click(object sender, System.EventArgs e)
        {
            if (CheckUserInput())
            {
                DataGridViewRow dgvSelectedRow = Categories_DGV.SelectedRows[0];
                Logic.XSD.UserSettings.CategoryRow catRow = Reader.UserSettings.Category.FindByNamePath(dgvSelectedRow.Cells["Category"].Value.ToString(), dgvSelectedRow.Cells["Path"].Value.ToString());

                catRow.Name = cmbCategory.SelectedItem.ToString();
                catRow.Path = txt_Path.Text;
                catRow.Extensions = txt_Extensions.Text;

                Reader.UserSettings.WriteXml(System.IO.Path.GetFullPath("XML/UserSettings.xml"));

                ClearControls();
                ToggleButtons(false);
            }
        }

        private void btn_Delete_Click(object sender, System.EventArgs e)
        {
            DataGridViewRow dgvSelectedRow = Categories_DGV.SelectedRows[0];
            Reader.UserSettings.Category.FindByNamePath(dgvSelectedRow.Cells["Category"].Value.ToString(), dgvSelectedRow.Cells["Path"].Value.ToString()).Delete();

            Reader.UserSettings.WriteXml(System.IO.Path.GetFullPath("XML/UserSettings.xml"));

            ClearControls();
            ToggleButtons(false);
        }

        private void Categories_DGV_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            btn_Add.Text = "New";
            ToggleButtons(true);

            DataGridViewRow dgvSelectedRow = Categories_DGV.Rows[e.RowIndex];

            cmbCategory.Text = dgvSelectedRow.Cells["Category"].Value.ToString();
            txt_Path.Text = dgvSelectedRow.Cells["Path"].Value.ToString();
            txt_Extensions.Text = dgvSelectedRow.Cells["Extensions"].Value.ToString();
        }

        #endregion Event Handlers

        private void btnLibraryLocation_Click(object sender, System.EventArgs e)
        {
            if (txtLibPath.Text != string.Empty)
                folderBrowserDialog_Path.SelectedPath = txtLibPath.Text;

            if (folderBrowserDialog_Path.ShowDialog(this) == DialogResult.OK)
                txtLibPath.Text = folderBrowserDialog_Path.SelectedPath;
        }

        private void txtLibPath_Leave(object sender, System.EventArgs e)
        {
            Settings.Default.LibraryDir = txtLibPath.Text;
            Settings.Default.Save();
        }

        private void txtLibName_Leave(object sender, System.EventArgs e)
        {
            Settings.Default.LibraryName = txtLibName.Text;
            Settings.Default.Save();
        }

        private void UserSettings_FormClosing(object sender, FormClosingEventArgs e)
        {
            if ((string.IsNullOrEmpty(txtLibPath.Text)) || string.IsNullOrEmpty(txtLibName.Text))
            {
                if (isFirstRun)
                {
                    DialogResult dgr = MessageBox.Show("Do you wish to cancel and exit?", "Exit Filesquirrel",
                                                       MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (dgr == DialogResult.No)
                    {
                        e.Cancel = true;
                    }
                    else
                    {
                        Application.Exit();
                    }
                }
                else
                {
                    MessageBox.Show("Please complete library name and location", "Information Needed!",
                                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                    e.Cancel = true;
                }
            }
            if (!e.Cancel)
            {
                Settings.Default.LibraryDir = txtLibPath.Text;
                Settings.Default.LibraryName = txtLibName.Text;
                Settings.Default.Save();
            }
        }

        private void cmbCategory_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            //AutoAssignExtensions();
        }

        private void AutoAssignExtensions()
        {
            switch (cmbCategory.Text.ToUpper())
            {
                case "SERIES":
                case "MOVIES":
                    txt_Extensions.Text = string.IsNullOrEmpty(txt_Extensions.Text) ? "avi;mkv;m4v;mp4;mpg" : txt_Extensions.Text;
                    break;
                default:
                    txt_Extensions.Text = string.Empty;
                    break;
            }
        }
    }
}